package com.assistant.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.provider.MediaStore;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.assistant.R;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.List;
import java.util.Map;

/**
 * Created by zhaokai on 2015/9/30.
 */
public class ListAdpter extends BaseAdapter {

    private LayoutInflater inflater;

    private Context context;
    private List<Map<String, Object>> data;

    public ListAdpter(Context context, List<Map<String, Object>> data) {
        super();
        this.inflater = LayoutInflater.from(context);
        this.context = context;
        this.data = data;
    }

    @Override
    public int getCount() {
        return data.size();
    }

    @Override
    public Object getItem(int position) {
        return this.data.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        Log.i("TAG",convertView+"");
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = inflater.inflate(R.layout.activity_contact, null);
            holder.user_logo = (ImageView) convertView.findViewById(R.id.user_logo);
            holder.user_name = (TextView) convertView.findViewById(R.id.user_name);
            holder.send_time = (TextView) convertView.findViewById(R.id.send_time);
            holder.msg = (TextView) convertView.findViewById(R.id.msg);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        Log.i("TAG",holder.user_logo+"");

        holder.user_logo.setImageResource(R.drawable.user_logo);
        holder.user_name.setText((String) data.get(position).get("user_name"));
        holder.send_time.setText((String) data.get(position).get("send_time"));
        holder.msg.setText((String) data.get(position).get("msg"));
        return convertView;
    }

    public final class ViewHolder {
        public ImageView user_logo;
        public TextView user_name;
        public TextView send_time;
        public TextView msg;
    }

    public void onDataChange(List<Map<String, Object>> data) {
        this.data.addAll(data);
        this.notifyDataSetChanged();
    }

}
